package dk.mirasola.systemtraining.bridgewidgets.shared.model.distributionfiltertree;

import com.google.gwt.user.client.rpc.IsSerializable;

public class CardsCountInterval implements IsSerializable {
    public static CardsCountInterval create(int min, int max, boolean validate) {
        if (validate) {
            CardsCountInterval cardsCountInterval = new CardsCountInterval();
            cardsCountInterval.setCardsCountInterval(min, max);
            return cardsCountInterval;
        } else {
            return new CardsCountInterval(min, max);
        }
    }

    public static final int CARDS_COUNT_INTERVAL_MIN = 0;
    public static final int CARDS_COUNT_INTERVAL_MAX = 13;

    private int minCardsCount = CARDS_COUNT_INTERVAL_MIN;
    private int maxCardsCount = CARDS_COUNT_INTERVAL_MAX;

    public CardsCountInterval() {
    }

    private CardsCountInterval(int minCardsCount, int maxCardsCount) {
        this.minCardsCount = minCardsCount;
        this.maxCardsCount = maxCardsCount;
    }

    public int getMinCardsCount() {
        return minCardsCount;
    }

    public int getMaxCardsCount() {
        return maxCardsCount;
    }

    public void setCardsCountInterval(int minCardsCount, int maxCardsCount) {
        if (minCardsCount < CARDS_COUNT_INTERVAL_MIN || minCardsCount > CARDS_COUNT_INTERVAL_MAX) {
            throw new IllegalArgumentException("min cards count must be between " + CARDS_COUNT_INTERVAL_MIN + " and " + CARDS_COUNT_INTERVAL_MAX);
        }
        if (maxCardsCount < CARDS_COUNT_INTERVAL_MIN || minCardsCount > CARDS_COUNT_INTERVAL_MAX) {
            throw new IllegalArgumentException("max cards count must be between " + CARDS_COUNT_INTERVAL_MIN + " and " + CARDS_COUNT_INTERVAL_MAX);
        }
        if (minCardsCount > maxCardsCount) {
            throw new IllegalArgumentException("min cards count must be maller than max cards count");
        }
        this.minCardsCount = minCardsCount;
        this.maxCardsCount = maxCardsCount;
    }

    boolean contains(int cardsCount) {
        return cardsCount >= minCardsCount && cardsCount <= maxCardsCount;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        CardsCountInterval that = (CardsCountInterval) o;
        return maxCardsCount == that.maxCardsCount && minCardsCount == that.minCardsCount;
    }

    @Override
    public int hashCode() {
        int result = minCardsCount;
        result = 31 * result + maxCardsCount;
        return result;
    }

    public CardsCountInterval copy() {
        return new CardsCountInterval(this.minCardsCount, this.maxCardsCount);
    }
}
